#pragma once

#include "topcvt_unpack.hpp"
#include "topcvt_pack.hpp"

#include "json_helper.hpp"

namespace topcvt
{
	/*
	*	<?xml version="1.0" encoding="GBK"?>
	*	<root>
	*		<policy type="json"  >
	*		</policy>
	*	</root>
	*/

	class JsonUnpack : public UnpackImp
	{
	private:
		JsonValue_t m_jsonDoc;
		bool m_traversal_parse;
		std::stack<JsonValue_t> m_jsonStack;
	public:
		JsonUnpack(ByteBuffer& message)
			: UnpackImp(message)
			, m_traversal_parse(true)
		{}
		void checkElementCfgValid(AttributeableCfg& t) override;
		virtual PolicyContext& unpackPolicy(PolicyContext& ctx, PolicyCfg& policyCfg) override;
		virtual string unpackField(pugi::xml_node& node, FieldCfg& f, PolicyContext& ctx) override;
		virtual void unpackStruct(pugi::xml_node& node, StructCfg& cfg, PolicyContext& ctx) override;
		virtual void unpackLoop(pugi::xml_node& node, LoopCfg& cfg, PolicyContext& ctx) override;		
	private:
		int unpackTraversal(PolicyContext& ctx, JsonValue_t& v);
	};

	class  JsonPack : public  PackImp
	{
	private:
		JsonValue_t m_jsonDoc;
		std::stack<JsonValue_t> m_jsonStack;
		bool m_traversal_parse;
	public:
		JsonPack() : m_traversal_parse(false) {}	
		virtual void checkElementCfgValid(AttributeableCfg& t) override;
		virtual string packPolicy(PolicyContext& policyContext, PolicyCfg& policyCfg) override;
		virtual void packField(pugi::xml_node& node, FieldCfg& f, PolicyContext& ctx) override;
		virtual void packLoop(pugi::xml_node& node, LoopCfg& cfg, PolicyContext& ctx) override;
		virtual void packStruct(pugi::xml_node& node, StructCfg& cfg, PolicyContext& ctx) override;
	private:
		int packTraversal(PolicyContext& ctx, PktObject& v, JsonValue_t& c);
	};
};